package com.sskj.gocoin.v.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.ExchangeHistoryEntry;
import com.sskj.gocoin.p.activity.ExchangeHistoryPresenter;
import com.sskj.gocoin.utils.TimeFormatUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import butterknife.BindView;

/**
 * 兑换历史
 */
public class ExchangeHistoryActivity extends BaseActivity<ExchangeHistoryPresenter> {

    @BindView(R2.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R2.id.exchange_history)
    RecyclerView exchangeHistory;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    private int page = 1;

    private BaseAdapter<ExchangeHistoryEntry.Entry> adapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exchange_history;
    }

    @Override
    public ExchangeHistoryPresenter getPresenter() {
        return new ExchangeHistoryPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setLeftButtonOnClickLinster(v -> {
            finish();
        });
        refresh.setRefreshHeader(new ClassicsHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setOnRefreshListener(v -> {
            page = 1;
            mPresenter.getHistory(page, 15);
        });
        refresh.setOnLoadMoreListener(v -> {
            page++;
            mPresenter.getHistory(page, 15);
        });
        exchangeHistory.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
                .setDividerColor(ContextCompat.getColor(this, R.color.transparent)));
        exchangeHistory.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<ExchangeHistoryEntry.Entry>(R.layout.item_exchange_history, null, exchangeHistory, true) {
            @Override
            public void bind(ViewHolder holder, ExchangeHistoryEntry.Entry item) {
                holder.setText(R.id.title, getString(R.string.exchange) + item.getPname());
                holder.setText(R.id.time, TimeFormatUtil.formatA(item.getAddtime() * 1000));
                holder.setText(R.id.exchange_num, item.getExnum());
                holder.setText(R.id.exchange_get, item.getExpname());
            }
        };
        exchangeHistory.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        mPresenter.getHistory(1, 15);
    }

    public void setData(ExchangeHistoryEntry data) {
        if (page == 1) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }


    public void stopRefresh() {
        refresh.finishLoadMore();
        refresh.finishRefresh();
    }
}
